package android.demo.service.aidl;

import java.util.List;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.demo.BaseActivity;
import android.demo.Constants.Action;
import android.demo.service.aidl.domain.Contact;
import android.demo.service.aidl.domain.ContactGroup;
import android.demo.service.aidl.impl.ContactGroupServiceImpl;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class ClientActivity extends BaseActivity {

	private IContactService contactService;
	private boolean isBindA;
	private boolean isBindB;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//布局
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
	    LayoutParams param = new LinearLayout.LayoutParams(250, 50);   
	    param.topMargin = 50;  
		
		//btnStart
		Button btnStart = new Button(this);
		btnStart.setText("启动远程服务");
		btnStart.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Log.i(TAG, "启动远程服务");
				startService(new Intent().setAction(Action.SERVICE_REMOTE_SERVICE));
			}
		});
		layout.addView(btnStart);	
		
		//btnStopRemote
		Button btnStop = new Button(this);
		btnStop.setText("停止远程服务");
		btnStop.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				stopService(new Intent().setAction(Action.SERVICE_REMOTE_SERVICE));
			}
		});
		layout.addView(btnStop);

		
		//btnBind_ContactService
		Button btnBind_ContactService = new Button(this);
		btnBind_ContactService.setText("绑定远程服务-ContactService");
		btnBind_ContactService.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				bindService(new Intent().setAction(Action.SERVICE_CONTACT_SERVICE), contactServiceConn, Service.BIND_AUTO_CREATE);
			}
		});
		layout.addView(btnBind_ContactService, param);
		
		Button btnUnbind_ContactService = new Button(this);
		btnUnbind_ContactService.setText("解除绑定远程服务");
		btnUnbind_ContactService.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				if (isBindA) {
					unbindService(contactServiceConn);
					isBindA = false;
					Log.i(TAG, "ContactService disconnected from activity!");
				}
			}
		});
		layout.addView(btnUnbind_ContactService);	
		
		//btnBind_ContactGroupService
		Button btnBind_ContactGroupService = new Button(this);
		btnBind_ContactGroupService.setText("绑定远程服务-ContactGroupService");
		btnBind_ContactGroupService.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				bindService(new Intent().setAction(Action.SERVICE_CONTACT_GROUP_SERVICE), contactGroupServiceConn, Service.BIND_AUTO_CREATE);
				
			}
		});
		layout.addView(btnBind_ContactGroupService, param);
		
		//btnUnbind_ContactGroupService
		Button btnUnbind_ContactGroupService = new Button(this);
		btnUnbind_ContactGroupService.setText("解除绑定远程服务");
		btnUnbind_ContactGroupService.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				if (isBindB) {
					unbindService(contactGroupServiceConn);
					isBindB = false;
					Log.i(TAG, "ContactGroupService disconnected from activity!");
				}
			}
		});
		layout.addView(btnUnbind_ContactGroupService);	
		
		//加载布局
		setContentView(layout);
	}
	
	private ServiceConnection contactServiceConn = new ServiceConnection() {
		
		public void onServiceDisconnected(ComponentName name) {
			Log.i(TAG, "ContactService disconnected!");
		}
		
		synchronized public void onServiceConnected(ComponentName name, IBinder service) {
			Log.i(TAG, "ContactService connected.");
			
			//获得IContactService接口
			contactService = IContactService.Stub.asInterface(service);
			if (contactService != null) {
				try {
					// RPC方法调用
					Contact[] contacts = contactService.getContacts("groupId");
					// 显示方法调用返回值
					for(Contact contact : contacts) {
						Log.i(TAG, contact.getContactId());
					}
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
			
			//
			isBindA = true;
		}
	};
	
	private ServiceConnection contactGroupServiceConn = new ServiceConnection() {
		
		public void onServiceDisconnected(ComponentName name) {
			Log.i(TAG, "ContactGroupService disconnected!");
		}
		
		synchronized public void onServiceConnected(ComponentName name, IBinder service) {
			Log.i(TAG, "ContactGroupService connected.");
			
			//获得ContactGroupService接口
			ContactGroupServiceImpl binder = (ContactGroupServiceImpl) service;     
			try {
				// RPC方法调用
				List<ContactGroup> list = binder.getContactGroupList();
				// 显示方法调用返回值
				for(ContactGroup contactGroup : list) {
					Log.i(TAG, contactGroup.getGroupName());
				}
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			
			//
			isBindB = true;
		}
	};
	
	protected void onDestroy() {
		super.onDestroy();
		if (isBindA) {
			unbindService(contactServiceConn);
		}
		if (isBindB) {
			unbindService(contactGroupServiceConn);
		}
	}
}
